package Engine;

import Covid.enums.ContactHistory;
import Covid.enums.DiagnosisResult;
import Covid.enums.Symptom;

import java.util.Set;

// CovidDiagnosisEngine.java
public class CovidDiagnosisEngine {
    private static final double FEVER_THRESHOLD = 37.3;
    private static final double HIGH_FEVER_THRESHOLD = 38.0;

    public DiagnosisResult diagnose(Set<Symptom> symptoms,
                                    Set<ContactHistory> contacts,
                                    Double temperature) {
        validateInput(symptoms, contacts);

        boolean hasSymptoms = hasTypicalSymptoms(symptoms);
        boolean hasRiskContact = hasRiskContact(contacts);
        boolean hasHighFever = isHighFever(temperature);

        if (hasSymptoms && hasRiskContact) {
            return DiagnosisResult.IMMEDIATE_HOSPITAL;
        } else if (hasSymptoms) {
            return hasHighFever ? DiagnosisResult.IMMEDIATE_HOSPITAL
                    : DiagnosisResult.HOME_OBSERVATION;
        } else if (hasRiskContact) {
            return DiagnosisResult.FAMILY_QUARANTINE;
        }
        return DiagnosisResult.GENERAL_PROTECTION;
    }

    private void validateInput(Set<Symptom> symptoms, Set<ContactHistory> contacts) {
        if (symptoms == null || contacts == null) {
            throw new IllegalArgumentException("症状和接触史不能为null");
        }
        if (!SymptomValidator.validate(symptoms)) {
            throw new IllegalArgumentException("症状选择冲突：选择了'无症状'时不能选择其他症状");
        }
    }

    private boolean hasTypicalSymptoms(Set<Symptom> symptoms) {
        return !symptoms.isEmpty() && !symptoms.contains(Symptom.NONE);
    }

    private boolean hasRiskContact(Set<ContactHistory> contacts) {
        return !contacts.isEmpty() && !contacts.contains(ContactHistory.NONE);
    }

    private boolean isHighFever(Double temp) {
        return temp != null && temp >= HIGH_FEVER_THRESHOLD;
    }
}

